//
//  WFRegisterViewController.m
//  WisdomFarm
//
//  Created by 心情 on 2017/10/26.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFRegisterViewController.h"
#import "WFTextFieldTableViewCell.h"
#import "UIButton+WF.h"
#import "UINavigationController+WFUtils.h"
#import "WFCaptchaButton.h"
#import "WFRegisterRequest.h"
#import "WFLoginPhoneCell.h"
#import "WFLoginPasswordCell.h"

@interface WFRegisterFooterView : UIView

@property (nonatomic, strong) UIButton *registerButton;

@end

@implementation WFRegisterFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _registerButton = [UIButton mainButtonWithTitle:@"注册"];
        [self addSubview:_registerButton];
        [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(40);
            make.left.right.insets(UIEdgeInsetsMake(0, 12, 0, 12));
            make.height.mas_equalTo(48);
        }];
    }
    return self;
}

@end

@interface WFRegisterViewController ()

@property (nonatomic, strong) NSArray<UITableViewCell *> *cells;

@property (nonatomic, strong) WFLoginPhoneCell *nameCell;
@property (nonatomic, strong) WFLoginPasswordCell *captchaCell;
@property (nonatomic, strong) WFLoginPasswordCell *passwordCell;

@property (nonatomic, strong) WFRegisterFooterView *footerView;

@property (nonatomic, strong) WFCaptchaButton *captchaButton;

@end

@implementation WFRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    @weakify(self);
    
    self.title = @"注册";
    
    UIImageView *bg = [[UIImageView alloc] init];
    bg.image = [UIImage imageNamed:@"login_bg"];
    self.tableView.backgroundView = bg;
    self.tableView.contentInset = UIEdgeInsetsMake(65, 0, 0, 0);
    self.tableView.allowsSelection = NO;
    
    self.nameCell = [WFLoginPhoneCell new];
    self.nameCell.textField.keyboardType = UIKeyboardTypePhonePad;
    self.nameCell.textField.placeholder = @"请输入11位手机号码";
    
    self.captchaButton = [WFCaptchaButton new];
    self.captchaButton.layer.cornerRadius = 4;
    [self.nameCell.contentView addSubview:self.captchaButton];
    self.nameCell.textField.keyboardType = UIKeyboardTypeNumberPad;
    [self.captchaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.insets(UIEdgeInsetsMake(13, 0, 13, 17+12));
        make.width.mas_equalTo(92);
    }];
    [self.captchaButton addTarget:self action:@selector(onCaptcha) forControlEvents:UIControlEventTouchUpInside];
    
    self.captchaCell = [WFLoginPasswordCell new];
    self.captchaCell.iconView.image = [UIImage imageNamed:@"enroll_enroll"];
    self.captchaCell.textField.placeholder = @"请输入手机验证码";
    
    self.passwordCell = [WFLoginPasswordCell new];
    self.passwordCell.textField.secureTextEntry = YES;
    self.passwordCell.textField.placeholder = @"请输入密码";
    
    self.cells = @[ self.nameCell, self.captchaCell, self.passwordCell ];
    
    self.footerView = [WFRegisterFooterView new];
    self.footerView.height = 89;
    self.tableView.tableFooterView = self.footerView;
    [self.footerView.registerButton addTarget:self action:@selector(onRegister) forControlEvents:UIControlEventTouchUpInside];

}

//MARK:- event
- (void)onCaptcha {
    WFCaptchaRequest *request = [WFCaptchaRequest new];
    request.mobileNum = self.nameCell.textField.text;
    [request startWithBlock:^(__kindof MZBaseRequest *request, NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            [self.captchaButton resetCoolDown];
            return;
        }
    }];
}

- (void)onRegister {
    WFRegisterRequest *request = [WFRegisterRequest new];
    request.phone = self.nameCell.textField.text;
    request.signCode = self.captchaCell.textField.text;
    request.password = [self.passwordCell.textField.text MD5];
    [request startWithBlock:^(__kindof MZBaseRequest *request, NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            return;
        }
        [SVProgressHUD showSuccessWithStatus:@"注册成功!"];
    }];
}

//MARK:- table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.cells[indexPath.row];
}

@end
